package com.phoenix.sgs.faq;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.phoenix.sgs.faq.R.string;
import com.phoenix.sgs.faq.general.GeneralHelper;
import com.phoenix.sgs.faq.meta.KeyValuePair;

public class CategoryGeneralActivity extends Activity {
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.categorygeneral);

        // Package
        Spinner spinnerPackage = (Spinner) findViewById(R.id.spinnerByPackage); 
        
        //将可选内容与ArrayAdapter连接起来   
        ArrayAdapter<KeyValuePair> adapterPackage = 
        	new ArrayAdapter<KeyValuePair>(
        			this,
        			android.R.layout.simple_spinner_item,
        			this.getPackages());   
           
        //设置下拉列表的风格   
        adapterPackage.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
           
        //将adapter 添加到spinner中   
        spinnerPackage.setAdapter(adapterPackage);   
        
        //添加事件Spinner事件监听     
        spinnerPackage.setOnItemSelectedListener(new SelectedListenerSpinnerPackage());   
           
        //设置默认值   
        spinnerPackage.setVisibility(View.VISIBLE);
        
        // Country
        Spinner spinnerCountry = (Spinner) findViewById(R.id.spinnerByCountry); 
        ArrayAdapter<KeyValuePair> adapterCountry = 
        	new ArrayAdapter<KeyValuePair>(
        			this,
        			android.R.layout.simple_spinner_item,
        			this.getCountries());   
        adapterCountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
        spinnerCountry.setAdapter(adapterCountry);   
        spinnerCountry.setOnItemSelectedListener(new SelectedListenerSpinnerCountry());   
        spinnerCountry.setVisibility(View.VISIBLE);  
    }
    
    class SelectedListenerSpinnerPackage implements OnItemSelectedListener{   
        public void onItemSelected(
        		AdapterView<?> parent, 
        		View view, 
        		int position,   
                long id) {   
        	Spinner spinnerPackage = (Spinner) view.getParent();
        	KeyValuePair selectedItem = (KeyValuePair) spinnerPackage.getSelectedItem();
        	if (selectedItem.getKey() > 0)
        		openGeneralListByPackage(selectedItem);  
        }   
  
        public void onNothingSelected(AdapterView<?> arg0) {   
        }   
    }
    
    class SelectedListenerSpinnerCountry implements OnItemSelectedListener{   
        public void onItemSelected(
        		AdapterView<?> parent, 
        		View view, 
        		int position,   
                long id) {   
        	Spinner spinnerPackage = (Spinner) view.getParent();
        	KeyValuePair selectedItem = (KeyValuePair) spinnerPackage.getSelectedItem();
        	if (selectedItem.getKey() > 0)
        		openGeneralListByCountry(selectedItem);  
        }   
  
        public void onNothingSelected(AdapterView<?> arg0) {   
        }   
    } 
    
    private KeyValuePair[] getPackages() {
    	List<KeyValuePair> packages = GeneralHelper.getPackages(this);
    	packages.add(0, new KeyValuePair(0, this.getResources().getString(string.query_method_bypackage)));
    	KeyValuePair[] result = packages.toArray(new KeyValuePair[packages.size()]);
    	
    	return result;
    }
    
    private KeyValuePair[] getCountries() {
    	List<KeyValuePair> countries = GeneralHelper.getCountries(this);
    	countries.add(0, new KeyValuePair(0, this.getResources().getString(string.query_method_bycountry)));
    	KeyValuePair[] result = countries.toArray(new KeyValuePair[countries.size()]);
    	
    	return result;
    }
    
    private void openGeneralListByPackage(KeyValuePair selectedItem) {
		Intent intent = new Intent(this, GeneralListActivity.class);
		intent.putExtra("QueryMethod", string.query_method_bypackage);
		intent.putExtra("SelectedId", selectedItem.getKey());
    	startActivity(intent);
    }
    
    private void openGeneralListByCountry(KeyValuePair selectedItem) {
		Intent intent = new Intent(this, GeneralListActivity.class);
		intent.putExtra("QueryMethod", string.query_method_bycountry);
		intent.putExtra("SelectedId", selectedItem.getKey());
    	startActivity(intent);
    }
}
